/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import org.jfonia.connect5.intervals.Interval;
import org.jfonia.connect5.intervals.LinkedDoubleIntervalList;
import org.jfonia.connect5.intervals.LinkedIntIntervalList;
import org.jfonia.connect5.intervals.LinkedLongIntervalList;

/**
 *
 * @author Jannes Plyson
 */
public class RemoveMultipleIntervals implements ICommand{
    private ArrayList<RemoveInterval> commands = new ArrayList<RemoveInterval>();

    public RemoveMultipleIntervals(Collection<Interval> intervals, LinkedIntIntervalList list){
        HashMap<Integer,Interval> vals = new HashMap<Integer, Interval>();
        for(Interval interval : intervals){
            vals.put(list.indexOf(interval), interval);            
        }
        SortedSet<Integer> sortedSet = new TreeSet<Integer>(vals.keySet());
        Iterator<Integer> it = sortedSet.iterator();        
        while(it.hasNext()){                        
            commands.add(new RemoveInterval(vals.get(it.next()), list));
        }
    }

    public RemoveMultipleIntervals(Collection<Interval> intervals, LinkedLongIntervalList list){
        HashMap<Integer,Interval> vals = new HashMap<Integer, Interval>();
        for(Interval interval : intervals){
            vals.put(list.indexOf(interval), interval);
        }
        SortedSet<Integer> sortedSet = new TreeSet<Integer>(vals.keySet());
        Iterator<Integer> it = sortedSet.iterator();
        while(it.hasNext()){
            commands.add(new RemoveInterval(vals.get(it.next()), list));
        }
    }

    public RemoveMultipleIntervals(Collection<Interval> intervals, LinkedDoubleIntervalList list){
        HashMap<Integer,Interval> vals = new HashMap<Integer, Interval>();
        for(Interval interval : intervals){
            vals.put(list.indexOf(interval), interval);
        }
        SortedSet<Integer> sortedSet = new TreeSet<Integer>(vals.keySet());
        Iterator<Integer> it = sortedSet.iterator();
        while(it.hasNext()){
            commands.add(new RemoveInterval(vals.get(it.next()), list));
        }
    }

    public void execute() {
        for(RemoveInterval command : commands){
            command.execute();
        }
    }

    public void undo() {
        for(int i = commands.size() -1; i >=0; i--){
            commands.get(i).undo();
        }                
    }
}
